/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.figure;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Defines the view of a use case displayed as an classifier
 * without its extension points.
 * 
 * @author Gilles Cannenterre
 * @since 8 nov. 06
 */
public class BasicUseCaseClassifierFigure extends NodeFigure {
	
	/**
	 * 
	 */
	protected static final int BORDER_MARGIN = 5;

	/**
	 * The container label figure.
	 */
	protected BasicUseCaseContainerLabelFigure containerLabelFigure; 
	
	/**
	 * The border figure.
	 */
	protected BasicBorderFigure borderFigure;

	/**
	 */
	protected boolean isBorderRequired = false;

	/**
	 * The Class UseCaseContainerLabelFigure.
	 * Holds the "extension points" string.
	 */
	protected class BasicUseCaseContainerLabelFigure extends NodeFigure {
		
		/**
		 * The Constructor.
		 */
		public BasicUseCaseContainerLabelFigure() {
			super();
			ToolbarLayout layout = new ToolbarLayout(ToolbarLayout.VERTICAL); 
			layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
			setLayoutManager(layout);
		}
	
		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){			
		}
		
		/**
		 * Paint figure.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintFigure(Graphics graphics){	
			if (isBorderRequired){
				containerLabelFigure.setName("extensions points");
			} else {
				containerLabelFigure.setName("");
			}
		}
	}


	/**
	 * The Class UseCaseBorderFigure.
	 * Holds the border line between name and property compartments.
	 */
	protected class BasicBorderFigure extends Figure {

		/**
		 * The Constructor.
		 */
		public BasicBorderFigure() {
			super();
			setSize(new Dimension(10,2*BORDER_MARGIN));
		}

		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){
			if (isBorderRequired){
				Point topLeft = getBounds().getTopLeft();
				Point topRight = getBounds().getTopRight();
				graphics.drawLine(topLeft.x,topLeft.y + BORDER_MARGIN,topRight.x,topRight.y + BORDER_MARGIN);
			}	
		}
	}


	/**
	 * The Constructor.
	 */
	public BasicUseCaseClassifierFigure() {
		super();
		ToolbarLayout layout = new ToolbarLayout(ToolbarLayout.VERTICAL);
		layout.setStretchMinorAxis(true);
		layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
		setLayoutManager(layout);	
		borderFigure = new BasicBorderFigure();
		add(borderFigure);
		containerLabelFigure = new BasicUseCaseContainerLabelFigure();
		add(containerLabelFigure);
	}
	
	/**
	 * Refresh colors.
	 * 
	 * @param graphElement the graph element
	 */
	@Override
	public void refreshColors(GraphElement graphElement) {
		super.refreshColors(graphElement);
		
		Font baseFont = super.getNameLabel().getFont();
		if (baseFont.getFontData() != null){
			FontData baseFontData = baseFont.getFontData()[0];
			FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.BOLD)};
			Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);	
			super.setFont(labelFont);
			containerLabelFigure.setFont(labelFont);
		}
		
		containerLabelFigure.setBackgroundColor(getBackgroundColor());
		containerLabelFigure.setFontColor(getFontColor());
		containerLabelFigure.setBorderColor(getBorderColor());
		containerLabelFigure.setForeGroundColor(getForeGroundColor());
		
		borderFigure.setForegroundColor(getBorderColor());
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public BasicBorderFigure getBorderFigure() {
		return borderFigure;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public BasicUseCaseContainerLabelFigure getContainerLabelFigure() {
		return containerLabelFigure;
	}

	/**
	 * 
	 * 
	 * @param isBorderRequired 
	 */
	public void setBorderRequired(boolean isBorderRequired) {
		this.isBorderRequired = isBorderRequired;
	}
}